/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-17
*/
package com.rzico.order.entity;

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;

/**
 * <pre>
 *   订单项数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@Table(name = "wx_order_item")
public class OrderItem{
    @ApiModelProperty(value = "")
    @Id
    protected Long id;

    @ApiModelProperty(value = "创建日期", hidden = true)
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期", hidden = true)
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "是否为赠品")
    protected Boolean isGift;

    @ApiModelProperty(value = "商品名称")
    protected String name;

    @ApiModelProperty(value = "单位")
    protected String unit;

    @ApiModelProperty(value = "商品重量")
    protected java.math.BigDecimal weight;

    @ApiModelProperty(value = "商品价格")
    protected java.math.BigDecimal price;

    @ApiModelProperty(value = "数量")
    protected BigDecimal quantity;

    @ApiModelProperty(value = "已退货数量")
    protected BigDecimal returnQuantity;

    @ApiModelProperty(value = "已发货数量")
    protected BigDecimal shippedQuantity;

    @ApiModelProperty(value = "商品规格")
    protected String spec;

    @ApiModelProperty(value = "类型 {0.产品,1:套餐,2:水票,3.压桶}" )
    protected Integer subType;

    @ApiModelProperty(value = "商品缩略图")
    protected String thumbnail;

    @ApiModelProperty(value = "订单")
    @Column(name = "order_id")
    protected Long orderId;

    @ApiModelProperty(value = "产品编号")
    @Column(name = "product_id")
    protected Long productId;

    @ApiModelProperty(value = "促销活动")
    @Column(name = "promotion_id")
    protected Long promotionId;

    @ApiModelProperty(value = "活动折扣金额")
    protected java.math.BigDecimal discount;

    @ApiModelProperty(value = "券抵扣数量")
    @Column(name = "coupon_code_quantity")
    protected Long couponCodeQuantity;

    @ApiModelProperty(value = "使用的券")
    @Column(name = "coupon_code_id")
    protected Long couponCodeId;

    @ApiModelProperty(value = "成本价")
    protected java.math.BigDecimal cost;

    @ApiModelProperty(value = "商品条码")
    protected String sn;

    @ApiModelProperty(value = "可发货数量")
    public BigDecimal getAvailableShippedQuantity() {

        if (getShippedQuantity()==null) {
            setShippedQuantity(BigDecimal.ZERO);
        }
        if (getReturnQuantity()==null) {
            setReturnQuantity(BigDecimal.ZERO);
        }
        if (getQuantity()==null) {
            setQuantity(BigDecimal.ZERO);
        }
        BigDecimal q = getQuantity().subtract(getShippedQuantity()).subtract(getReturnQuantity());
        if (q.compareTo(BigDecimal.ZERO)<0) {
            return BigDecimal.ZERO;
        } else {
            return q;
        }

    }

    @ApiModelProperty(value = "可退货数量")
    public BigDecimal getAvailableReturnQuantity() {

        if (getShippedQuantity()==null) {
            setShippedQuantity(BigDecimal.ZERO);
        }
        if (getReturnQuantity()==null) {
            setReturnQuantity(BigDecimal.ZERO);
        }
        if (getQuantity()==null) {
            setQuantity(BigDecimal.ZERO);
        }
        BigDecimal q = getQuantity().subtract(getReturnQuantity());
        if (q.compareTo(BigDecimal.ZERO)<0) {
            return BigDecimal.ZERO;
        } else {
            return q;
        }

    }

    public void setAvailableShippedQuantity(BigDecimal value) {

    }

    public void setAvailableReturnQuantity(BigDecimal value) {

    }

    @ApiModelProperty(value = "商品金额")
    public BigDecimal getAmount() {
        return this.getPrice().multiply(this.getQuantity()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
    }

    @ApiModelProperty(value = "实收金额")
    public BigDecimal getSubTotal() {
        if (getDiscount()==null) {
            setDiscount(BigDecimal.ZERO);
        }
        return getAmount().subtract(getDiscount()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
    }

    public BigDecimal calculateSubCost() {
        return this.getCost().multiply(this.getQuantity()).setScale(2,BigDecimal.ROUND_HALF_DOWN);
    }

    public void setAmount(BigDecimal value) {

    }

    public void setSubTotal(BigDecimal value) {

    }

}
